#include "StatusBar.h"
#include "GameFont.h"
#include "RenderShape.h"
#include "../data/GameInfo.h"
#include "../manager/PlayerManager.h"

void StatusBar::OnRender()
{
    if (!isShow) return;
    RenderHead();
    RenderPlayerHp();
    RenderAttribute();
}

void StatusBar::RenderHead() const
{
    auto player = PlayerManager::Instance().GetPlayer();
    SDL_Color colorBackground = {48, 40, 51, 255};
    SDL_Rect srcRect = player->animStandBy[0].rectSrcList[0];
    SDL_Texture *tex = player->animStandBy[0].texture;
    SDL_Rect dstRect = {(int) position.x, (int) position.y, 65, 65};
    roundedBoxRGBA(GAME_RENDERER, dstRect.x, dstRect.y, dstRect.x + dstRect.w, dstRect.y + dstRect.h, 4,
                   colorBackground.r, colorBackground.g, colorBackground.b, colorBackground.a);
    SDL_RenderCopy(GAME_RENDERER, tex, &srcRect, &dstRect);
}

void StatusBar::RenderPlayerHp() const
{
    auto player = PlayerManager::Instance().GetPlayer();

    SDL_Color colorBackground = {48, 40, 51, 255};
    SDL_Color colorForeground = {255, 0, 0, 255};
    RenderShape::RendererHp(GAME_RENDERER, {position.x + 80, position.y}, player->hp, player->maxHp, colorBackground,
                            colorForeground);

    colorForeground = {144, 121, 173, 255};
    RenderShape::RendererHp(GAME_RENDERER, {position.x + 80, position.y + 35}, player->mp, player->maxMp,
                            colorBackground, colorForeground);
}

void StatusBar::RenderAttribute() const
{
    auto player = PlayerManager::Instance().GetPlayer();
    GameFont font;
    std::string hpStr = "血量: " + std::to_string((int) player->hp) + '/' + std::to_string((int) player->maxHp);
    std::string mpStr = "蓝量: " + std::to_string((int) player->mp) + '/' + std::to_string((int) player->maxMp);
    std::string coinsStr = "金币: " + std::to_string(GAME_INFO.coins);
    std::string enemySize = "敌人数量: " + std::to_string(GAME_INFO.enemies.size());
    std::string time = "时间: " + std::to_string((int)GAME_INFO.gameTime) + "s";
    font.OnRender(GAME_RENDERER, GAME_INFO.font, {(int) position.x + 470, (int) position.y + 10}, {0, 0, 0}, hpStr);
    font.OnRender(GAME_RENDERER, GAME_INFO.font, {(int) position.x + 470, (int) position.y + 50}, {0, 0, 0}, mpStr);
    font.OnRender(GAME_RENDERER, GAME_INFO.font, {(int) position.x + 700, (int) position.y + 10}, {0, 0, 0}, coinsStr);
    font.OnRender(GAME_RENDERER, GAME_INFO.font, {(int) position.x + 1000, (int) position.y + 10}, {0, 0, 0}, time);
    font.OnRender(GAME_RENDERER, GAME_INFO.font, {(int) position.x + 1000, (int) position.y + 50}, {0, 0, 0}, enemySize);
}